package com.fashion.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * User: Administrator
 * Date: 11-12-18
 * Time: 下午4:50
 */
public class ManagementUtils {

    public static int calculatePageNumber(int totalRows, int pageSize) {
        int pageNumber = totalRows / pageSize;

        if ((totalRows % pageSize) > 0) {
            pageNumber++;
        }

        return pageNumber;
    }

    /**
         * 传递一个字符串参数，然后采用MD5进行加密
         * @return 返回一个加密字符串
         */
        public static String encryptWithMD5(String initialString){
            MessageDigest md5 = null;
            char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                    'a', 'b', 'c', 'd', 'e', 'f' };
            try {
                byte[] strTemp = initialString.getBytes("UTF-8");//得到字节数组
                md5 = MessageDigest.getInstance("MD5");//得到MessageDigest 对象
                md5.reset();
                md5.update(strTemp);
                byte[] md = md5.digest();
                int k = md.length;
                char[] chars = new char[k*2];
                int n = 0;
                for(int i = 0; i < k ;i++){
                    byte bytes0 = md[i];
                    chars[n++] = hexDigits[bytes0 >>> 4 & 0xf];
                    chars[n++] = hexDigits[bytes0 & 0xf];
                }
                return new String(chars);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return "";
        }
}
